package cn.com.cifi.mars.util;


import cn.com.cifi.mars.constant.BigStandardFormatsEnum;
import cn.com.cifi.mars.constant.StandardFormatsMapEnum;
import cn.com.cifi.mars.entity.DmCityReturnCoefficient;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;

/**
 * 城市回款系数分析表模版
 *
 * @author ztz
 * @version 1.0
 * @date 2021/1/5 9:51
 */
@Slf4j
public class CityReturnCoefficientTemplate {

    private static final String TEMPLATE_PATH = "/template/city_return_coefficient.xlsx";

    private XSSFWorkbook workbook;

    private XSSFSheet sheet;

    //默认sheet索引
    private int defaultSheetIndex = 0;
    //数据开始行
    private int dataBeginRow = 1;
    //当前行标
    private int currentDataRowCursor = dataBeginRow;
    //单元格样式
    private XSSFCellStyle cellStyle;
    //文字样式
    private XSSFCellStyle cellStyle1;
    //数值样式
    private XSSFCellStyle cellStyle2;

    private BigDecimal wan = new BigDecimal(10000);


    public CityReturnCoefficientTemplate(String name) {

        try {
            InputStream tempInputStream = CityReturnCoefficientTemplate.class.getResourceAsStream(TEMPLATE_PATH);
            if (tempInputStream != null) {
                workbook = new XSSFWorkbook(tempInputStream);
                sheet = workbook.getSheetAt(defaultSheetIndex);
                setSheetDate(name);
                XSSFRow row = sheet.getRow(dataBeginRow);
                cellStyle1 = row.getCell(0).getCellStyle();
                cellStyle2 = row.getCell(5).getCellStyle();

            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }


    public void setSheetDate(String name) {
        workbook.setSheetName(defaultSheetIndex, workbook.getSheetName(defaultSheetIndex).replace("Sheet1", name));
    }

    /**
     * 批量插入行
     *
     * @param list 报表数据
     */
    public synchronized void appendRows(List<DmCityReturnCoefficient> list) {
        if(CollectionUtils.isEmpty(list)){
            //删除项目行的数据
            sheet.shiftRows(2, 2, -1);
        }
        for (DmCityReturnCoefficient cityVo : list) {
            this.appendRow(cityVo);
        }
    }

    /**
     * 插入行
     *
     * @param
     */
    public synchronized void appendRow(DmCityReturnCoefficient cityVo) {
        XSSFRow row = sheet.getRow(currentDataRowCursor);
        if (row == null) {
            row = sheet.createRow(currentDataRowCursor);
        }
        int index = 0;

        cellStyle = cellStyle1;
        this.appendCell(row, index++, cityVo.getCityName());
        this.appendCell(row, index++,cityVo.getRegionName());
        if (BigStandardFormatsEnum.getkeyList().contains(cityVo.getProProductCode())) {

            this.appendCell(row, index++, cityVo.getProProductType());
            this.appendCell(row, index++, "");
            this.appendCell(row, index++, cityVo.getProProductCode());
        } else {
            this.appendCell(row, index++, BigStandardFormatsEnum.getValueByKey(StandardFormatsMapEnum.getValueByKey(cityVo.getProProductCode())));
            this.appendCell(row, index++, cityVo.getProProductType());
            this.appendCell(row, index++, cityVo.getProProductCode());
        }
        cellStyle = cellStyle2;

        this.appendCell(row, index++, cityVo.getOneMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTwoMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getThreeMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getFourMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getFiveMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getSixMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getSevenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getEightMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getNineMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getElevenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTwelveMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getThirteenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getFourteenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getFifteenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getSixteenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getSeventeenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getEighteenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getNineteenMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTwentyMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTwentyOneMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTwentyTwoMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTwentyThreeMonth().doubleValue());
        this.appendCell(row, index++, cityVo.getTwentyFourMonth().doubleValue());

        currentDataRowCursor++;

    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, String value) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value == null ? "" : value);
    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, Double value) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }

    /**
     * 导出
     *
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workbook.write(outputStream);
    }
}


